
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _utils = require('../../utils');

const _constants = require('../constants');

const _stepper = _interopRequireDefault(require('../../stepper'));

const namespace = (0, _utils.createNamespace)('sku-stepper');
const createComponent = namespace[0];
const t = namespace[2];
const { QUOTA_LIMIT } = _constants.LIMIT_TYPE;
const { STOCK_LIMIT } = _constants.LIMIT_TYPE;

const _default = createComponent({
  props: {
    stock: Number,
    skuEventBus: Object,
    skuStockNum: Number,
    selectedNum: Number,
    stepperTitle: String,
    disableStepperInput: Boolean,
    customStepperConfig: Object,
    hideQuotaText: Boolean,
    quota: {
      type: Number,
      default: 0,
    },
    quotaUsed: {
      type: Number,
      default: 0,
    },
    startSaleNum: {
      type: Number,
      default: 1,
    },
  },
  data: function data() {
    return {
      currentNum: this.selectedNum,
      // 购买限制类型: 限购/库存
      limitType: STOCK_LIMIT,
    };
  },
  watch: {
    currentNum: function currentNum(num) {
      const intValue = parseInt(num, 10);

      if (intValue >= this.stepperMinLimit && intValue <= this.stepperLimit) {
        this.skuEventBus.$emit('sku:numChange', intValue);
      }
    },
    stepperLimit: function stepperLimit(limit) {
      if (limit < this.currentNum && this.stepperMinLimit <= limit) {
        this.currentNum = limit;
      }

      this.checkState(this.stepperMinLimit, limit);
    },
    stepperMinLimit: function stepperMinLimit(start) {
      if (start > this.currentNum || start > this.stepperLimit) {
        this.currentNum = start;
      }

      this.checkState(start, this.stepperLimit);
    },
  },
  computed: {
    stepperLimit: function stepperLimit() {
      const quotaLimit = this.quota - this.quotaUsed;
      let limit; // 无限购时直接取库存，有限购时取限购数和库存数中小的那个

      if (this.quota > 0 && quotaLimit <= this.stock) {
        // 修正负的limit
        limit = quotaLimit < 0 ? 0 : quotaLimit;
        this.limitType = QUOTA_LIMIT;
      } else {
        limit = this.stock;
        this.limitType = STOCK_LIMIT;
      }

      return limit;
    },
    stepperMinLimit: function stepperMinLimit() {
      return this.startSaleNum < 1 ? 1 : this.startSaleNum;
    },
    quotaText: function quotaText() {
      const _this$customStepperCo = this.customStepperConfig;
      const { quotaText } = _this$customStepperCo;
      const { hideQuotaText } = _this$customStepperCo;
      if (hideQuotaText) return '';
      let text = '';

      if (quotaText) {
        text = quotaText;
      } else {
        const textArr = [];

        if (this.startSaleNum > 1) {
          textArr.push(t('quotaStart', this.startSaleNum));
        }

        if (this.quota > 0) {
          textArr.push(t('quotaLimit', this.quota));
        }

        text = textArr.join(t('comma'));
      }

      return text;
    },
  },
  created: function created() {
    this.checkState(this.stepperMinLimit, this.stepperLimit);
  },
  methods: {
    setCurrentNum: function setCurrentNum(num) {
      this.currentNum = num;
      this.checkState(this.stepperMinLimit, this.stepperLimit);
    },
    onOverLimit: function onOverLimit(action) {
      this.skuEventBus.$emit('sku:overLimit', {
        action,
        limitType: this.limitType,
        quota: this.quota,
        quotaUsed: this.quotaUsed,
        startSaleNum: this.startSaleNum,
      });
    },
    onChange: function onChange(currentValue) {
      const intValue = parseInt(currentValue, 10);
      const { handleStepperChange } = this.customStepperConfig;
      handleStepperChange && handleStepperChange(intValue);
      this.$emit('change', intValue);
    },
    checkState: function checkState(min, max) {
      // 如果选择小于起售，则强制变为起售
      if (this.currentNum < min || min > max) {
        this.currentNum = min;
      } else if (this.currentNum > max) {
        // 当前选择数量大于最大可选时，需要重置已选数量
        this.currentNum = max;
      }

      this.skuEventBus.$emit('sku:stepperState', {
        valid: min <= max,
        min,
        max,
        limitType: this.limitType,
        quota: this.quota,
        quotaUsed: this.quotaUsed,
        startSaleNum: this.startSaleNum,
      });
    },
  },
  render: function render() {
    const _this = this;

    const h = arguments[0];
    return h('div', {
      class: 'van-sku-stepper-stock',
    }, [h('div', {
      class: 'van-sku-stepper-container',
    }, [h('div', {
      class: 'van-sku__stepper-title',
    }, [this.stepperTitle || t('num')]), h(_stepper.default, {
      class: 'van-sku__stepper',
      attrs: {
        min: this.stepperMinLimit,
        max: this.stepperLimit,
        disableInput: this.disableStepperInput,
        integer: true,
      },
      on: {
        overlimit: this.onOverLimit,
        change: this.onChange,
      },
      model: {
        value: _this.currentNum,
        callback: function callback($$v) {
          _this.currentNum = $$v;
        },
      },
    }), !this.hideQuotaText && this.quotaText && h('span', {
      class: 'van-sku__stepper-quota',
    }, ['(', this.quotaText, ')'])])]);
  },
});

exports.default = _default;
